package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 晒单方式
 */
public class BeiDan_BF_Pop extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private View view;
	private TextView cancel_tv;
	private TextView sure_tv;

	private TextView home_tv;
	private TextView away_tv;

	private CheckBox sheng1;
	private CheckBox sheng2;
	private CheckBox sheng3;
	private CheckBox sheng4;
	private CheckBox sheng5;
	private CheckBox sheng6;
	private CheckBox sheng7;
	private CheckBox sheng8;
	private CheckBox sheng9;
	private CheckBox sheng10;

	private CheckBox ping1;
	private CheckBox ping2;
	private CheckBox ping3;
	private CheckBox ping4;
	private CheckBox ping5;

	private CheckBox fu1;
	private CheckBox fu2;
	private CheckBox fu3;
	private CheckBox fu4;
	private CheckBox fu5;
	private CheckBox fu6;
	private CheckBox fu7;
	private CheckBox fu8;
	private CheckBox fu9;
	private CheckBox fu10;

	private List<CheckBox> checkBoxes = new ArrayList<>();

	private BeiDanChildBean infor;

	private OnBeiDanBfPopListener listener;

	public BeiDan_BF_Pop(Context context, BeiDanChildBean infor, OnBeiDanBfPopListener listener){
		super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		this.context = context;
		this.infor = infor;
		this.listener = listener;
		View view_pop = LayoutInflater.from(context).inflate(R.layout.popup_beidan_bf, null);
		this.view = view_pop;
		this.view.setOnTouchListener(this);
		initView();
		this.setContentView(view);
		this.setFocusable(true);
		this.setOutsideTouchable(false);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.keyboard_anim);
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
//		this.dismiss();
//		popupDismiss(true);
		return true;
	}

	private void initView(){

		home_tv = view.findViewById(R.id.beidan_bf_pop_home);
		away_tv = view.findViewById(R.id.beidan_bf_pop_away);

		sheng1 = view.findViewById(R.id.beidan_bf_pop_sheng1);
		sheng2 = view.findViewById(R.id.beidan_bf_pop_sheng2);
		sheng3 = view.findViewById(R.id.beidan_bf_pop_sheng3);
		sheng4 = view.findViewById(R.id.beidan_bf_pop_sheng4);
		sheng5 = view.findViewById(R.id.beidan_bf_pop_sheng5);
		sheng6 = view.findViewById(R.id.beidan_bf_pop_sheng6);
		sheng7 = view.findViewById(R.id.beidan_bf_pop_sheng7);
		sheng8 = view.findViewById(R.id.beidan_bf_pop_sheng8);
		sheng9 = view.findViewById(R.id.beidan_bf_pop_sheng9);
		sheng10 = view.findViewById(R.id.beidan_bf_pop_sheng10);

		ping1 = view.findViewById(R.id.beidan_bf_pop_ping1);
		ping2 = view.findViewById(R.id.beidan_bf_pop_ping2);
		ping3 = view.findViewById(R.id.beidan_bf_pop_ping3);
		ping4 = view.findViewById(R.id.beidan_bf_pop_ping4);
		ping5 = view.findViewById(R.id.beidan_bf_pop_ping5);

		fu1 = view.findViewById(R.id.beidan_bf_pop_fu1);
		fu2 = view.findViewById(R.id.beidan_bf_pop_fu2);
		fu3 = view.findViewById(R.id.beidan_bf_pop_fu3);
		fu4 = view.findViewById(R.id.beidan_bf_pop_fu4);
		fu5 = view.findViewById(R.id.beidan_bf_pop_fu5);
		fu6 = view.findViewById(R.id.beidan_bf_pop_fu6);
		fu7 = view.findViewById(R.id.beidan_bf_pop_fu7);
		fu8 = view.findViewById(R.id.beidan_bf_pop_fu8);
		fu9 = view.findViewById(R.id.beidan_bf_pop_fu9);
		fu10 = view.findViewById(R.id.beidan_bf_pop_fu10);

		checkBoxes.clear();
		checkBoxes.add(sheng1);
		checkBoxes.add(sheng2);
		checkBoxes.add(sheng3);
		checkBoxes.add(sheng4);
		checkBoxes.add(sheng5);
		checkBoxes.add(sheng6);
		checkBoxes.add(sheng7);
		checkBoxes.add(sheng8);
		checkBoxes.add(sheng9);
		checkBoxes.add(sheng10);

		checkBoxes.add(ping1);
		checkBoxes.add(ping2);
		checkBoxes.add(ping3);
		checkBoxes.add(ping4);
		checkBoxes.add(ping5);

		checkBoxes.add(fu1);
		checkBoxes.add(fu2);
		checkBoxes.add(fu3);
		checkBoxes.add(fu4);
		checkBoxes.add(fu5);
		checkBoxes.add(fu6);
		checkBoxes.add(fu7);
		checkBoxes.add(fu8);
		checkBoxes.add(fu9);
		checkBoxes.add(fu10);

		cancel_tv = view.findViewById(R.id.beidan_bf_pop_cancel);
		sure_tv = view.findViewById(R.id.beidan_bf_pop_sure);

		cancel_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupDismiss(true);
			}
		});

		sure_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				popupDismiss(false);

				infor.isbf_10 = sheng1.isChecked();
				infor.isbf_20 = sheng2.isChecked();
				infor.isbf_21 = sheng3.isChecked();
				infor.isbf_30 = sheng4.isChecked();
				infor.isbf_31 = sheng5.isChecked();
				infor.isbf_32 = sheng6.isChecked();
				infor.isbf_40 = sheng7.isChecked();
				infor.isbf_41 = sheng8.isChecked();
				infor.isbf_42 = sheng9.isChecked();
				infor.isbf_90 = sheng10.isChecked();

				infor.isbf_00 = ping1.isChecked();
				infor.isbf_11 = ping2.isChecked();
				infor.isbf_22 = ping3.isChecked();
				infor.isbf_33 = ping4.isChecked();
				infor.isbf_99 = ping5.isChecked();

				infor.isbf_01 = fu1.isChecked();
				infor.isbf_02 = fu2.isChecked();
				infor.isbf_12 = fu3.isChecked();
				infor.isbf_03 = fu4.isChecked();
				infor.isbf_13 = fu5.isChecked();
				infor.isbf_23 = fu6.isChecked();
				infor.isbf_04 = fu7.isChecked();
				infor.isbf_14 = fu8.isChecked();
				infor.isbf_24 = fu9.isChecked();
				infor.isbf_09 = fu10.isChecked();

				String result = "";
				infor.checkNum = 0;
				for (int i = 0; i < checkBoxes.size(); i++) {
					if (checkBoxes.get(i).isChecked()) {
						infor.checkNum++;
						result += checkBoxes.get(i).getText().toString().split("\n")[0]+" ";
					}
				}
				if (result.length()>0){
					result = result.substring(0, result.length()-1);
				}
				if (!result.equals("")) {
					infor.bf_zk = result;
				} else {
					infor.bf_zk = BeiDanChildBean.BF_ZK_DEFAULT;
				}

				if (listener != null) {
					listener.onSureClick();
				}
			}
		});

		initData();
	}

	private void initData() {
		home_tv.setText(infor.getHomeTeam());
		away_tv.setText(infor.getAwayTeam());

		sheng1.setText("1:0\n"+infor.getBf_10());
		sheng2.setText("2:0\n"+infor.getBf_20());
		sheng3.setText("2:1\n"+infor.getBf_21());
		sheng4.setText("3:0\n"+infor.getBf_30());
		sheng5.setText("3:1\n"+infor.getBf_31());
		sheng6.setText("3:2\n"+infor.getBf_32());
		sheng7.setText("4:0\n"+infor.getBf_40());
		sheng8.setText("4:1\n"+infor.getBf_41());
		sheng9.setText("4:2\n"+infor.getBf_42());
		sheng10.setText("胜其它\n"+infor.getBf_90());

		ping1.setText("0:0\n"+infor.getBf_00());
		ping2.setText("1:1\n"+infor.getBf_11());
		ping3.setText("2:2\n"+infor.getBf_22());
		ping4.setText("3:3\n"+infor.getBf_33());
		ping5.setText("平其它\n"+infor.getBf_99());

		fu1.setText("0:1\n"+infor.getBf_01());
		fu2.setText("0:2\n"+infor.getBf_02());
		fu3.setText("1:2\n"+infor.getBf_12());
		fu4.setText("0:3\n"+infor.getBf_03());
		fu5.setText("1:3\n"+infor.getBf_13());
		fu6.setText("2:3\n"+infor.getBf_23());
		fu7.setText("0:4\n"+infor.getBf_04());
		fu8.setText("1:4\n"+infor.getBf_14());
		fu9.setText("2:4\n"+infor.getBf_24());
		fu10.setText("负其它\n"+infor.getBf_09());

		sheng1.setChecked(infor.isbf_10);
		sheng2.setChecked(infor.isbf_20);
		sheng3.setChecked(infor.isbf_21);
		sheng4.setChecked(infor.isbf_30);
		sheng5.setChecked(infor.isbf_31);
		sheng6.setChecked(infor.isbf_32);
		sheng7.setChecked(infor.isbf_40);
		sheng8.setChecked(infor.isbf_41);
		sheng9.setChecked(infor.isbf_42);
		sheng10.setChecked(infor.isbf_90);

		ping1.setChecked(infor.isbf_00);
		ping2.setChecked(infor.isbf_11);
		ping3.setChecked(infor.isbf_22);
		ping4.setChecked(infor.isbf_33);
		ping5.setChecked(infor.isbf_99);

		fu1.setChecked(infor.isbf_01);
		fu2.setChecked(infor.isbf_02);
		fu3.setChecked(infor.isbf_12);
		fu4.setChecked(infor.isbf_03);
		fu5.setChecked(infor.isbf_13);
		fu6.setChecked(infor.isbf_23);
		fu7.setChecked(infor.isbf_04);
		fu8.setChecked(infor.isbf_14);
		fu9.setChecked(infor.isbf_24);
		fu10.setChecked(infor.isbf_09);
	}


	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent, BeiDanChildBean infor) {
		this.infor = infor;
		if (!this.isShowing()) {
			super.showAtLocation(parent, Gravity.BOTTOM,0,0);
		} else {
			this.dismiss();
		}

		initData();
	}

	private void popupDismiss(boolean isCancel){
		if (!isCancel) {

		} else {

		}
		this.dismiss();
	}

	@Override
	public void dismiss() {
		super.dismiss();
	}

	//===== 接口	==========
	public interface OnBeiDanBfPopListener {
		void onSureClick();
	}


}
